package com.examiner.service.impl;

import com.examiner.bean.CommonPerson;
import com.examiner.bean.Department;
import com.examiner.bean.Job;
import com.examiner.bean.vo.JobLowerVo;
import com.examiner.dao.CommonPersonDao;
import com.examiner.dao.DepartmentDao;
import com.examiner.dao.JobDao;
import com.examiner.service.JobService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class JobServiceImpl implements JobService {

    @Resource
    private JobDao jobDao;

    @Resource
    private DepartmentDao departmentDao;

    @Resource
    private CommonPersonDao commonPersonDao;

    /**
     * 根据id查询当前职位
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Job getJobById(Integer id) throws Exception {
        return jobDao.getOne(id);
    }

    /**
     * 根据id删除当前职位
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void deleteJobById(Integer id) throws Exception {
        Job job = jobDao.getOne(id);
        deleteChildren(job);
        jobDao.deleteById(id);
    }

    private void deleteChildren(Job job){
        // 下级职位
        List<Job> jobList =  jobDao.findBySidAndStype(job.getId(),job.getType());
        if (jobList.size()!= 0 ){
            jobList.forEach(item -> {
                deleteChildren(item);
                jobDao.deleteById(item.getId());
            });
        }
        // 下级成员
        List<CommonPerson> commonPersonList = commonPersonDao.findBySIdAndStype(job.getId(),job.getType());
        if (commonPersonList.size() != 0){
            commonPersonList.forEach(item -> commonPersonDao.deleteById(item.getId()));
        }
        // 下级部门
        List<Department> departmentList = departmentDao.findBySIdAndStype(job.getId(),job.getType());
        if (departmentList.size() != 0){
            departmentList.forEach(item -> {
                List<CommonPerson> bySIdAndStype = commonPersonDao.findBySIdAndStype(item.getId(), item.getType());
                if (bySIdAndStype.size() != 0){
                    bySIdAndStype.forEach(item1 -> commonPersonDao.deleteById(item1.getId()));
                }
                departmentDao.deleteById(item.getId());
            });
        }
    }

    /**
     * 修改职位信息
     * @param job
     * @throws Exception
     */
    @Override
    public void changeJobInfo(Job job) throws Exception {
        Job jobDaoOne = jobDao.getOne(job.getId());
        jobDaoOne.setUserName(job.getUserName());
        jobDao.save(jobDaoOne);
    }

    /**
     * 新增职位
     * @param job
     * @throws Exception
     */
    @Override
    public void addJob(Job job) throws Exception {
        job.setType("职位");
        jobDao.save(job);
    }

    /**
     * 根据userName查询职位
     * @param userName
     * @return
     * @throws Exception
     */
    @Override
    public Job getJobByUserName(String userName) throws Exception {
        return jobDao.findByUserName(userName);
    }

    /**
     * 查询职位下级信息
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public JobLowerVo getLower(Integer id) throws Exception {
        JobLowerVo jobLowerVo = new JobLowerVo();
        Job job = jobDao.getOne(id);
        if (id == 0){
            Job job1 = jobDao.findBySid(0);
            List<Job> jobs = new ArrayList<>();
            jobs.add(job1);
            jobLowerVo.setJobList(jobs);
            return jobLowerVo;
        }
        // 下级职位
        List<Job> jobList =  jobDao.findBySidAndStype(job.getId(),job.getStype());
        // 下级成员
        List<CommonPerson> commonPersonList = commonPersonDao.findBySIdAndStype(job.getId(),job.getStype());
        // 下级部门
        List<Department> departmentList = departmentDao.findBySIdAndStype(job.getId(),job.getStype());
        if (jobList != null){jobLowerVo.setJobList(jobList);}
        if (commonPersonList != null){jobLowerVo.setCommonPersonList(commonPersonList);}
        if (departmentList != null){jobLowerVo.setDepartmentList(departmentList);}
        return jobLowerVo;
    }

    /**
     * 获取所有下级
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public JobLowerVo getAllLower(Integer id) throws Exception {
        JobLowerVo jobLowerVo = new JobLowerVo();
        jobLowerVo.setJobList(new ArrayList<>());
        jobLowerVo.setDepartmentList(new ArrayList<>());
        jobLowerVo.setCommonPersonList(new ArrayList<>());
        getAllJobLower(jobLowerVo, jobDao.getOne(id));
        return jobLowerVo;
    }

    private void getAllJobLower( JobLowerVo jobLowerVo,Job job){
        // 下级职位
        List<Job> jobList =  jobDao.findBySidAndStype(job.getId(),job.getType());
        if (jobList.size()!= 0 ){
            jobList.forEach(item -> {
                getAllJobLower(jobLowerVo,item);
                jobLowerVo.getJobList().add(item);
            });
        }
        // 下级成员
        List<CommonPerson> commonPersonList = commonPersonDao.findBySIdAndStype(job.getId(),job.getType());
        if (commonPersonList.size() != 0){
            jobLowerVo.getCommonPersonList().addAll(commonPersonList);
        }
        // 下级部门
        List<Department> departmentList = departmentDao.findBySIdAndStype(job.getId(),job.getType());
        if (departmentList.size() != 0){
            departmentList.forEach(item -> {
                List<CommonPerson> bySIdAndStype = commonPersonDao.findBySIdAndStype(item.getId(), item.getType());
                if (bySIdAndStype.size() != 0){
                    jobLowerVo.getCommonPersonList().addAll(bySIdAndStype);
                }
                jobLowerVo.getDepartmentList().add(item);
            });
        }
    }


}
